package com.cloud.adrian.novel.home.service.impl;

import com.cloud.adrian.novel.common.resp.RestResp;
import com.cloud.adrian.novel.home.dao.entity.HomeBook;
import com.cloud.adrian.novel.home.dao.mapper.HomeBookMapper;
import com.cloud.adrian.novel.home.dto.resp.HomeBookRespDto;
import com.cloud.adrian.novel.home.dto.resp.HomeFriendLinkRespDto;
import com.cloud.adrian.novel.home.manager.cache.FriendLinkCacheManager;
import com.cloud.adrian.novel.home.manager.cache.HomeBookCacheManager;
import com.cloud.adrian.novel.home.service.HomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 小说推荐 服务实现类
 * </p>
 *
 * @author xiangguiliang
 * @since 2024-05-17
 */
@Service
@RequiredArgsConstructor
public class HomeServiceImpl extends ServiceImpl<HomeBookMapper, HomeBook> implements HomeService {

    private final HomeBookCacheManager homeBookCacheManager;

    private final FriendLinkCacheManager friendLinkCacheManager;

    @Override
    public RestResp<List<HomeBookRespDto>> listHomeBooks() {
        List<HomeBookRespDto> list = homeBookCacheManager.listHomeBooks();
        if (CollectionUtils.isEmpty(list)) {
            // 为空清理缓存，方便下一次加载数据
            homeBookCacheManager.evictCache();
        }
        return RestResp.ok(list);
    }

    @Override
    public RestResp<List<HomeFriendLinkRespDto>> listHomeFriendLinks() {
        return RestResp.ok(friendLinkCacheManager.listFriendLinks());
    }
}
